package com.example.wangli.myservice;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;

public class MusicService extends Service {
    private MediaPlayer player;
    private String mp3Path = "";
    private Boolean isPause = false;
    public MusicService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mp3Path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/mp3/";
        player = new MediaPlayer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)  {
        String command = intent.getStringExtra("action");
        switch (command){
            case "play":
                try {
                    play();
                }
                catch (Exception e){
                }
                break;
            case "pause":
                pause();
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    public void play() throws  Exception{
        if(!isPause){
            player.reset();
            player.setDataSource(mp3Path+"th.mp3");
            player.prepare();
        }
        player.start();
        isPause = false;
    }
    public void pause(){
        player.pause();
        isPause = true;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
